package com.zhanghe.im.handler;

import com.zhanghe.im.protocol.command.PacketCodeC;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

import java.nio.ByteOrder;

/**
 * Spliter
 *
 * @author Clevo
 * @date 2018/10/30
 */
public class Spliter extends LengthFieldBasedFrameDecoder {
    private static final int LENGTH_FIELD_OFFSET = 7;
    private static final int LENGTH_FIELD_LENGTH = 4;

    public Spliter(){
        super(Integer.MAX_VALUE, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {

        //只处理本协议的客户端连接
        if(in.getInt(in.readerIndex())!=PacketCodeC.MAGIC_NUM){
            ctx.channel().close();
            return null;
        }
        return super.decode(ctx, in);
    }
}
